#include "qartiklivnes.h"
#include <QSqlQuery>
#include <QMessageBox>
#include "chelperclass.h"
#include <QDesktopWidget>
#include <QSqlError>



QArtikliVnes::QArtikliVnes(QWidget *parent)
	: QMyBaseFormWidget(parent)
{
	ui.setupUi(this);
	QDesktopWidget desk;
	QRect deskRect = desk.screenGeometry();
	ui.layoutWidget->setFixedWidth(deskRect.width() - 300);
//	ui.layoutWidget->setFixedHeight(deskRect.height()-200);
	ui.lineEdit_2->setFocus();
	ui.lineEdit_2->setStyleSheet("background-color: yellow");
	ui.lineEdit_2->installEventFilter(this);
	ui.lineEdit_3->installEventFilter(this);
	ui.lineEdit_4->installEventFilter(this);
	ui.lineEdit_5->installEventFilter(this);
	ui.lineEdit_6->installEventFilter(this);
	ui.lineEdit_7->installEventFilter(this);
	ui.lineEdit_8->installEventFilter(this);
	ui.lineEdit_9->installEventFilter(this);
	ui.lineEdit_10->installEventFilter(this);
	ui.lineEdit_11->installEventFilter(this);
	ui.lineEdit->installEventFilter(this);

	CHelperClass hlp(this);
    ui.lineEdit_2->setText(QString::number(hlp.genSifraArtikal(), 10));
    ui.lineEdit_3->setFocus();
	connect(ui.lineEdit_7, SIGNAL(editingFinished()), this, SLOT(lineEdit7EditingFinished()));
	connect(ui.lineEdit_8, SIGNAL(editingFinished()), this, SLOT(lineEdit8EditingFinished()));
	connect(ui.lineEdit_9, SIGNAL(editingFinished()), this, SLOT(lineEdit9EditingFinished()));
	connect(ui.lineEdit_10, SIGNAL(editingFinished()), this, SLOT(lineEdit10EditingFinished()));

}


void QArtikliVnes::setSLine(QString text)
{
    ui.lineEdit_3->setText(text);
}


void QArtikliVnes::lineEdit7EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_7->text());
	ui.lineEdit_8->setText(loc.toString(cena1 * (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_7->setText(loc.toString(cena1, 'f', 2));
}

void QArtikliVnes::lineEdit8EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_8->text());
	ui.lineEdit_7->setText(loc.toString(cena1 / (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_8->setText(loc.toString(cena1, 'f', 2));
}

void QArtikliVnes::lineEdit9EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_9->text());
	ui.lineEdit_10->setText(loc.toString(cena1 * (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_9->setText(loc.toString(cena1, 'f', 2));
}

void QArtikliVnes::lineEdit10EditingFinished()
{
	QLocale loc;
	float ddv = loc.toFloat(ui.lineEdit_11->text());
	float cena1 = loc.toFloat(ui.lineEdit_10->text());
	ui.lineEdit_9->setText(loc.toString(cena1 / (1 + ddv / 100.f), 'f', 2));
	ui.lineEdit_10->setText(loc.toString(cena1, 'f', 2));
}

QArtikliVnes::~QArtikliVnes()
{

}


void QArtikliVnes::on_pushButton_clicked()
{
		if (ui.lineEdit_3->text().isEmpty())
		{
			QMessageBox msgBox;
			msgBox.setText(trUtf8("Внеси назив на артикал!"));
			msgBox.setStandardButtons(QMessageBox::Ok);
			msgBox.setDefaultButton(QMessageBox::Ok);
			msgBox.exec();
			ui.lineEdit_3->setFocus();
			return;
		}

	
		QMessageBox msgBox;
		msgBox.setText(trUtf8("Документот ќе биде внесен."));
		msgBox.setInformativeText(trUtf8("Дали сакате да ги сочувате промените?"));
		msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
		msgBox.setDefaultButton(QMessageBox::Ok);
		int ret = msgBox.exec();
        QLocale loc;
		if (ret == QMessageBox::Ok )
		{
			QSqlQuery query;
			query.prepare("insert into artikli (sifra, artikal, edm, ref, kataloski_broj, art_nabavna_cena, art_nabavna_cena_valuta, art_prodazna_cena, art_prodazna_cena_valuta, ddv, proizvoditel, kategorija)" 
				"values(:sifra, :art, :edm, :ref, :kb, :anc, :ancv, :apc, :apcv, :iddvm, :iproizvoditel, :ikategorija)");
			query.bindValue(":sifra", ui.lineEdit_2->text().toInt());
			query.bindValue(":art", ui.lineEdit_3->text());
			query.bindValue(":edm", ui.lineEdit_4->text());
			query.bindValue(":ref", ui.lineEdit_5->text());
			query.bindValue(":kb", ui.lineEdit_6->text());
			query.bindValue(":anc", loc.toFloat(ui.lineEdit_7->text()));
			query.bindValue(":ancv", loc.toFloat(ui.lineEdit_8->text()));
			query.bindValue(":apc", loc.toFloat(ui.lineEdit_9->text()));
			query.bindValue(":apcv", loc.toFloat(ui.lineEdit_10->text()));
			query.bindValue(":iddv", loc.toFloat(ui.lineEdit_11->text()));
			query.bindValue(":iproizvoditel", ui.lineEdit->text());
            query.bindValue(":ikategorija", ui.checkBox->isChecked() ? "1" : "0");

			if (query.exec())
			{
				emit succesfulEntryData();
			}
			QSqlError er = query.lastError();
		}
}
void QArtikliVnes::pressEscape()
{
	emit closeW();
}

void QArtikliVnes::pressReturn()
{
    if (ui.lineEdit_2->hasFocus())
    {
        ui.lineEdit_3->setFocus();
    }
    else if (ui.lineEdit_3->hasFocus())
    {
        ui.lineEdit->setFocus();
    }
	else if (ui.lineEdit->hasFocus())
	{
		procCallProizvoditel();
		//ui.lineEdit_4->setFocus();
	}
	else if (ui.lineEdit_4->hasFocus())
    {
        ui.lineEdit_5->setFocus();
    }
    else if (ui.lineEdit_5->hasFocus())
    {
        ui.lineEdit_6->setFocus();
    }
    else if (ui.lineEdit_6->hasFocus())
    {
        ui.lineEdit_11->setFocus();
    }
    else if (ui.lineEdit_7->hasFocus())
    {
        ui.lineEdit_8->setFocus();
    }
    else if (ui.lineEdit_8->hasFocus())
    {
        ui.lineEdit_9->setFocus();
    }
    else if (ui.lineEdit_9->hasFocus())
    {
        ui.lineEdit_10->setFocus();
    }
    else if (ui.lineEdit_10->hasFocus())
    {
		on_pushButton_clicked();
    }
	else if (ui.lineEdit_11->hasFocus())
    {
		ui.lineEdit_7->setFocus();
    }
}

bool QArtikliVnes::eventFilter(QObject *object, QEvent *event)
{
	if (event->type() == QEvent::FocusIn)
	{
		if (object == ui.lineEdit_2)
		{
			ui.lineEdit_2->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_3)
		{
			ui.lineEdit_3->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_4)
		{
			ui.lineEdit_4->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_5)
		{
			ui.lineEdit_5->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_6)
		{
			ui.lineEdit_6->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_7)
		{
			ui.lineEdit_7->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_8)
		{
			ui.lineEdit_8->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_9)
		{
			ui.lineEdit_9->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_10)
		{
			ui.lineEdit_10->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit_11)
		{
			ui.lineEdit_11->setStyleSheet("background-color: yellow");
		}
		if (object == ui.lineEdit)
		{
			ui.lineEdit->setStyleSheet("background-color: yellow");
		}

	}
	if (event->type() == QEvent::FocusOut)
	{
		if (object == ui.lineEdit_2)
		{
			ui.lineEdit_2->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_3)
		{
			ui.lineEdit_3->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_4)
		{
			ui.lineEdit_4->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_5)
		{
			ui.lineEdit_5->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_6)
		{
			ui.lineEdit_6->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_7)
		{
			ui.lineEdit_7->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_8)
		{
			ui.lineEdit_8->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_9)
		{
			ui.lineEdit_9->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_10)
		{
			ui.lineEdit_10->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit_11)
		{
			ui.lineEdit_11->setStyleSheet("background-color: none");
		}
		if (object == ui.lineEdit)
		{
			ui.lineEdit->setStyleSheet("background-color: none");
		}
	}
	return false;
}

void QArtikliVnes::pressF9()
{
	on_pushButton_clicked();
}


void QArtikliVnes::procCallProizvoditel()
{

	CHelperClass hclass(this);
	if (!hclass.isKomintentExists(ui.lineEdit->text()))
	{
		emit scallKomintenti((QWidget*)this, ui.lineEdit->text());
	}
	else
	{
		ui.lineEdit_4->setFocus();
	}
}

void QArtikliVnes::setKomintent(QString text, QString textName)
{
	ui.lineEdit->setText(textName);
}


void QArtikliVnes::setFocusLine()
{
	ui.lineEdit_4->setFocus();
}

